package com.woniuxy.platform.core.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.common.CommonResult;
import com.woniuxy.platform.core.model.PmAdvertising;
import com.woniuxy.platform.core.service.PmAdvertisingService;
import com.woniuxy.platform.core.vo.PmAdvertisingVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gmx
 * @since 2021-07-14
 */
@RestController
@RequestMapping("/pmAdvertising")
@Api(tags = "广告接口")
public class PmAdvertisingController {

    @Resource
    PmAdvertisingService advertisingService;


    /**
     * Description : 查询所有广告  <br/>
     * ChangeLog : 1. 创建 (2021/7/14 11:23 [Hello Mr. Guo]);
     * @param startpage
     * @param pagesize
     * @return com.woniuxy.common.CommonResult<com.github.pagehelper.PageInfo>
     */
    @GetMapping("list")
    @ApiOperation("查询所有广告")
    public CommonResult<PageInfo> list(int startpage, int pagesize){
        PageHelper.startPage(startpage, pagesize);
        Page<PmAdvertising> page = (Page<PmAdvertising>) this.advertisingService.list();
        PageInfo<PmAdvertising> pageInfo = new PageInfo<>(page);
        return CommonResult.success(pageInfo);
    }


    /**
     * Description : 对广告进行模糊查询  <br/>
     * ChangeLog : 1. 创建 (2021/7/14 14:37 [Hello Mr. Guo]);
     * @param pmAdvertisingVo
     * @return com.woniuxy.common.CommonResult<com.github.pagehelper.PageInfo>
    */
    @PostMapping("selectMH")
    @ApiOperation("对广告进行模糊查询")
    public CommonResult<PageInfo> selectMH(@RequestBody PmAdvertisingVo pmAdvertisingVo){
        PageHelper.startPage(pmAdvertisingVo.getPage(),pmAdvertisingVo.getLimit());
        Page<PmAdvertising> page = (Page<PmAdvertising>) this.advertisingService.selectMH(pmAdvertisingVo.getPmAdvertising());
        PageInfo<PmAdvertising> pageInfo = new PageInfo<>(page);
        return CommonResult.success(pageInfo);

    }


    /**
     * Description : 更改广告  <br/>
     * ChangeLog : 1. 创建 (2021/7/14 14:57 [Hello Mr. Guo]);
     * @param pmAdvertising
     * @return com.woniuxy.common.CommonResult
    */
    @PostMapping("update")
    @ApiOperation("更改广告信息")
    public CommonResult update(@RequestBody PmAdvertising pmAdvertising){
        boolean b = this.advertisingService.updateById(pmAdvertising);
        if (b){
            return CommonResult.success();
        }
        return CommonResult.fail();

    }


    /**
     * Description : 新增  <br/>
     * ChangeLog : 1. 创建 (2021/7/14 14:59 [Hello Mr. Guo]);
     * @param pmAdvertising
     * @return com.woniuxy.common.CommonResult
    */
    @PostMapping("insert")
    @ApiOperation("新增广告")
    public CommonResult insert(@RequestBody PmAdvertising pmAdvertising){
        int save = this.advertisingService.insert(pmAdvertising);
        if (save > 0){
            return CommonResult.success();
        }
        return CommonResult.fail();
    }

    /**
     * Description : 删除广告
     * ChangeLog : 1. 创建 (2021/7/12 9:49 [fulang]);
     * @param id
     * @return booleane
     */
    @ApiOperation("删除广告")
    @GetMapping("delete/{id}")
    public CommonResult delete(@PathVariable Integer id){
        boolean b = advertisingService.removeById(id);
        if (b){
            return CommonResult.success();
        }
        return CommonResult.fail();
    };


    @ApiOperation("发布广告")
    @PostMapping("updateStatus")
    public CommonResult updateStatus(@RequestBody PmAdvertising pmAdvertising){
        int i = this.advertisingService.updateStatus(pmAdvertising);
        if (i>0){
            return CommonResult.success();
        }
        return CommonResult.fail();
    }


    @ApiOperation("取消发布广告")
    @PostMapping("updateDisStatus")
    public CommonResult updateDisStatus(@RequestBody PmAdvertising pmAdvertising){
        int i = this.advertisingService.updateDisStatus(pmAdvertising);
        if (i>0){
            return CommonResult.success();
        }
        return CommonResult.fail();
    }

}

